// home/home.js
const {bindLoginName} = require('../../apiConfig/apiList/luban')
const {saveSubscribeStatus} = require('../../apiConfig/apiList/luban')
const md5 = require('md5')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // loginName: '15219361604',//账号
    // password: '123456',//密码
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('md5', md5)
  },
  getLoginName: function(e) {
    this.setData({
      loginName: e.detail.value
    })
  },
  getPassword: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  getCodeValue: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  //登录
  toLogin: function (e) {
    
    let that = this
    let password = md5(that.data.password + that.data.loginName)
    console.log("password:",password)
    if(!wx.getStorageSync('loginData').maOpenId){
      wx.navigateTo({
        url: '/pages/home/index',
      })
    }
    bindLoginName({
      loginName: that.data.loginName,
      password: password,
      openId: wx.getStorageSync('loginData').maOpenId,
    }).then(res=>{
      if (res.code == 200) {
        // wx.setStorageSync('token', res.token);
        let temp = wx.getStorageSync('loginData');
        temp.authToken = res.data.authToken;
        temp.loginUser = res.data.loginUser;
        wx.setStorageSync('loginData', temp);
        //用户消息
        wx.requestSubscribeMessage({
          tmplIds: ['pCb1mSBy3HK0cU5PjHxMfL497L5rgFFslVo86utkZ8Q'],
          success (res) {
            if(res.errMsg == 'requestSubscribeMessage:ok'){
              let params2 = {
                subscribeStatus: '2'
              };
              saveSubscribeStatus(params2).then(res=>{
                console.log('saveSubscribeStatus', res);
                
              })
            }
           }
          })
        wx.navigateTo({
          url: '/pages/order-flow/index',
        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 1000
        })
      }

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  clear:function(){
    this.setData({
      loginName:""
    })
  }
  
})